package com.hboxs.asl.controller.app.member;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.CourseTrade;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.CourseTradeService;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller(value = "appMCourseTradeController")
@RequestMapping(value = "/app/m/courseTrade")
public class CourseTradeController extends BaseController {

    @Resource(name = "courseTradeServiceImpl")
    private CourseTradeService courseTradeService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    /**
     * 获取所有订单
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/allTrade", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage allTrade(Pageable pageable, Integer status) {
        try {
            Member member = memberService.getCurrentByToken();
            if(member == null) {
                return JsonMessage.USER_NOT_LOGIN;
            }
            Page<CourseTrade> page = new Page<>();
            if(status.equals(0)) {
                //所有课程状态（包括评价和未评价）
                page = courseTradeService.allTrade(pageable, member, CourseTrade.PaymentStatus.paid);
            } else if(status.equals(1)) {
                //所有非录播状态
                page = courseTradeService.ing(pageable, member, CourseTrade.CourseType.finished, CourseTrade.PaymentStatus.paid);
            } else if(status.equals(2)) {
                //所有录播状态（未评价）
                page = courseTradeService.toEvaluate(pageable, member, CourseTrade.CourseType.finished, CourseTrade.PaymentStatus.paid, false);
            }
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "categoryName","id", "title", "courseId", "imageURL", "price", "sn", "hasReview",
                            "courseType", "teacherId", "teacherName", "teacherAvator", "password"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
